package com.xintonghua.hongyi.ui.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.xintonghua.base.BuildConfig;
import com.xintonghua.base.ui.BaseFragment;
import com.xintonghua.base.ui.ImageActivity;
import com.xintonghua.base.ui.MySubscriber;
import com.xintonghua.base.utils.MyUtils;
import com.xintonghua.base.utils.RefreshUtils;
import com.xintonghua.hongyi.R;
import com.xintonghua.hongyi.adapter.HomeAdapter;
import com.xintonghua.hongyi.ui.home.LocationActivity;

import java.util.ArrayList;
import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import io.reactivex.Flowable;
import io.reactivex.android.schedulers.AndroidSchedulers;

public class HomeFragment extends BaseFragment implements XRecyclerView.LoadingListener {

    @BindView(R.id.lv_home)
    XRecyclerView lvHome;
    HomeAdapter adapter;
    @BindView(R.id.tv_saoyisao)
    TextView tvSaoyisao;
    @BindView(R.id.tv_day)
    ImageView tvDay;
    @BindView(R.id.iv_message)
    ImageView ivMessage;
    Unbinder unbinder;

    @Override
    public int getLayout() {
        return R.layout.fragment_home;
    }

    @Override
    public void initUI(View view) {
        ButterKnife.bind(this, view);
        RefreshUtils.initList(getActivity(), lvHome);
        View head = LayoutInflater.from(getContext()).inflate(R.layout.layout_home_header, lvHome, false);
        lvHome.addHeaderView(head);
        adapter = new HomeAdapter(getActivity(), new ArrayList<String>());
        lvHome.setAdapter(adapter);
        lvHome.setLoadingListener(this);

    }


    @OnClick({R.id.tv_saoyisao, R.id.tv_day, R.id.iv_message})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_saoyisao:
                MyUtils.openActivity(getActivity(), LocationActivity.class);
                break;
            case R.id.tv_day:
                break;
            case R.id.iv_message:
                Bundle bundle = new Bundle();
                bundle.putInt(BuildConfig.FLAVOR, R.mipmap.user_message);
                MyUtils.openActivity(getActivity(), ImageActivity.class, bundle);
                break;
        }
    }


    @Override
    public void onRefresh() {
        Flowable.timer(300, TimeUnit.MILLISECONDS).observeOn(AndroidSchedulers.mainThread()).subscribe(new MySubscriber<Long>() {
            @Override
            public void next(Long aLong) {
                adapter.notifyDataSetChanged();
                lvHome.refreshComplete();
            }
        });

    }

    @Override
    public void onLoadMore() {
        lvHome.loadMoreComplete();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder = ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

}
